package com.chengxiaoxiao.libarypartner.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.activity.BookActivity;
import com.chengxiaoxiao.libarypartner.constants.AppConsrants;
import com.chengxiaoxiao.libarypartner.domain.BookImageAddress;
import com.chengxiaoxiao.libarypartner.utils.ApiAddress;
import com.chengxiaoxiao.libarypartner.utils.LoadDialogUtils;
import com.chengxiaoxiao.libarypartner.utils.ToastUtils;
import com.chengxiaoxiao.libarypartner.utils.UserUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.image.SmartImageView;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class FindFragment extends Fragment
{
    List<BookImageAddress> listSimple = new ArrayList<>();

    private ListView listGuess;
    private String[] listNames = new String[]{"新书", "新书榜", "新书上架", "热门作者", "猜你喜欢", "新书", "新书榜", "新书上架", "热门作者"};
    private int[] listIcon = {R.drawable.fragment_index_book1, R.drawable.fragment_index_book2, R.drawable.fragment_index_book3, R.drawable.fragment_index_book4, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_find, container, false);

        listGuess = (ListView) view.findViewById(R.id.listGuess);

        LoadDialogUtils.show(getActivity(),"","加载中...");
        initRecommend();




        //listGuess.setAdapter(new MyListAdapter());

        listGuess.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Intent intent = new Intent(getActivity(), BookActivity.class);

                intent.putExtra(AppConsrants.BOOK_ID,listSimple.get(position).getId()+"");
                startActivity(intent);
            }
        });

        return view;
    }

    /**
     * 加载推荐信息
     */
    private void initRecommend()
    {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get(ApiAddress.getApiDomain() + "/client/ClientServlet?op=recommend&uid=" + UserUtils.getNumber(getActivity()), new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {
                LoadDialogUtils.hidden();

                String body = new String(responseBody);
                try
                {
                    JSONArray jsonArray = new JSONArray(body);


                    for (int i = 0; i < jsonArray.length(); i++)
                    {

                        JSONObject jsonObj = jsonArray.getJSONObject(i);

                        String address = jsonObj.getString("address");
                        String bookName = jsonObj.getString("bookName");
                        String imageSrc = jsonObj.getString("imageSrc");
                        int id = jsonObj.getInt("id");

                        BookImageAddress bia = new BookImageAddress(id, bookName, imageSrc, address);
                        listSimple.add(bia);
                    }
                    listGuess.setAdapter(new MyListAdapter());


                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                LoadDialogUtils.hidden();
                ToastUtils.makeText(getActivity(), "获取新版本失败");
            }
        });
    }

    class MyListAdapter extends BaseAdapter
    {


        @Override
        public int getCount()
        {
            return listSimple.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listSimple.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View view;
            if (convertView == null)
            {
                view = View.inflate(getActivity(), R.layout.list_item_guess, null);
            } else
            {
                view = convertView;
            }

            BookImageAddress b = listSimple.get(position);

            TextView textName = (TextView) view.findViewById(R.id.textName);
            SmartImageView image = (SmartImageView) view.findViewById(R.id.ivIcon);
            TextView textAddress = (TextView) view.findViewById(R.id.textAddress);

            textName.setText(b.getBookName());
            image.setImageUrl(b.getImageSrc());
            textAddress.setText(b.getAddress());

            image.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent(getActivity(), BookActivity.class);

                    intent.putExtra(AppConsrants.BOOK_ID,listSimple.get(position).getId()+"");
                    startActivity(intent);
                }
            });

            return view;
        }
    }


}
